﻿namespace UploadWinClient
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public partial class TaskPanel : Panel
    {
        public bool AutoSizeForm = true; 
        public List<TaskPanelItem> List = new List<TaskPanelItem>();
        private Point NextPanelPoint = new Point(0, 0);
        public bool RemoveItemsOnError = false;
        public bool RemoveItemsWhenFinished = false;
        private int TaskPanelItemHeight = 70;

        public TaskPanel()
        {
            InitializeComponent();
            this.TaskPanelItemHeight = new TaskPanelItem().Height;
        }

        public void AddOperation(TaskPanelOperation op)
        {
            lock (this)
            {
                Debug.WriteLine(string.Format("Adding panel with guid {0} for task {1}", op.Guid, op.Text));
                TaskPanelItem item = new TaskPanelItem(op);
                op.Status = TaskStatus.Waiting;
                item.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
                item.Location = this.NextPanelPoint;
                item.Width = base.Width - 30;
                item.pictureBox1.Image = this.imageList1.Images[1];
                base.Controls.Add(item);
                this.List.Add(item);
                //Debug.WriteLine(item.Height + "::" + this.TaskPanelItemHeight);
                if (this.AutoSizeForm && (this.List.Count > 1))
                {
                    base.Parent.Height = Math.Min(550, 80 + (this.TaskPanelItemHeight * this.List.Count));
                }
                this.NextPanelPoint.Y += this.TaskPanelItemHeight;
            }
        }


        public void EndOperation(string Guid, Exception ex)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new EndOperationDelegate(this.EndOperation), new object[] { Guid, ex });
            }
            else
            {
                lock (this)
                {
                    Debug.WriteLine(string.Format("Removing panel with guid {0}", Guid));
                    TaskPanelItem item = base.Controls["pnl" + Guid.Replace("-", "")] as TaskPanelItem;
                    if (item == null)
                    {
                        Debug.WriteLine("Error task panel item not found for guid " + Guid);
                    }
                    else
                    {
                        item.progressBar1.Visible = false;
                        if (ex == null)
                        {
                            item.op.Status = TaskStatus.Success;
                            item.pictureBox1.Image = this.imageList1.Images[1];
                            item.lblStatus.Text = "Complete";
                        }
                        else
                        {
                            item.op.Status = TaskStatus.Failed;
                            item.pictureBox1.Image = this.imageList1.Images[2];
                            item.lblStatus.Text = ex.Message;
                            //Debug.WriteLine(ex.Message);
                        }
                        if (this.RemoveItemsWhenFinished && (this.RemoveItemsOnError || (ex == null)))
                        {
                            TaskPanelItem item2 = null;
                            int num = 0;
                            foreach (TaskPanelItem item3 in this.List)
                            {
                                num++;
                                if (item3.op.Guid == Guid)
                                {
                                    item2 = item3;
                                    break;
                                }
                            }
                            if (item2 != null)
                            {
                                this.List.Remove(item2);
                                num--;
                            }
                            Control control = null;
                            foreach (Control control2 in base.Controls)
                            {
                                if (control2.Name == ("pnl" + Guid.Replace("-", "")))
                                {
                                    control = control2;
                                    break;
                                }
                            }
                            if (control != null)
                            {
                                base.Controls.Remove(control);
                            }
                            this.NextPanelPoint.Y -= this.TaskPanelItemHeight;
                            for (int i = num; i < this.List.Count; i++)
                            {
                                string str = "pnl" + this.List[i].op.Guid.Replace("-", "");
                                if (base.Controls[str] != null)
                                {
                                    Control control1 = base.Controls[str];
                                    control1.Top -= this.TaskPanelItemHeight+30;
                                }
                            }
                        }
                    }
                }
            }
        }


        public void ProgressChanged(string Guid, int ProgressPercentage, string Message)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new ProgressChangedDelegate(this.ProgressChanged), new object[] { Guid, ProgressPercentage, Message });
            }
            else
            {
                string str = "pnl" + Guid.Replace("-", "");
                if (base.Controls[str] != null)
                {
                    TaskPanelItem item = base.Controls[str] as TaskPanelItem;
                    item.lblStatus.Text = Message;
                    if ((ProgressPercentage > 0) && (ProgressPercentage < item.progressBar1.Maximum))
                    {
                        item.progressBar1.Value = ProgressPercentage;
                        item.progressBar1.Visible = Message.ToLower().IndexOf("waiting") < 0;
                    }
                    item.pictureBox1.Image = this.imageList1.Images[0];
                }
            }
        }

        public void StartOperation(string Guid)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new StartOperationDelegate(this.StartOperation), new object[] { Guid });
            }
            else
            {
                lock (this)
                {
                    TaskPanelItem item = base.Controls["pnl" + Guid.Replace("-", "")] as TaskPanelItem;
                    if (item == null)
                    {
                        Debug.WriteLine("Error task panel item not found for guid " + Guid);
                    }
                    else
                    {
                        item.op.Status = TaskStatus.Running;
                        item.pictureBox1.Image = this.imageList1.Images[0];
                        item.progressBar1.Visible = true;
                    }
                }
            }
        }

        private delegate void EndOperationDelegate(string Guid, Exception ex);

        private delegate void ProgressChangedDelegate(string Guid, int ProgressPercentage, string Message);

        private delegate void StartOperationDelegate(string Guid);
    }
}

